package edu.czjtu.geesonke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.czjtu.geesonke.common.R;
import edu.czjtu.geesonke.entity.Employee;
import edu.czjtu.geesonke.entity.Shop;
import edu.czjtu.geesonke.service.ShopService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by jinkun.tian on 2023/10/6
 */
@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Shop> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Shop::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Shop::getUpdateTime);

        //执行查询
        shopService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Shop shop) {
        log.info("新增店铺，店铺信息：{}", shop.toString());

        shopService.save(shop);
        return R.success("新增店铺成功");
    }

    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Shop shop) {
        log.info(shop.toString());
        shopService.updateById(shop);
        return R.success("店铺信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Shop> getById(@PathVariable Long id) {
        log.info("根据id查询员工信息...");
        Shop shop = shopService.getById(id);
        if (shop != null) {
            return R.success(shop);
        }
        return R.error("没有查询到对应店铺信息");
    }
}
